﻿unit MVC.ORM;

interface

uses
  System.SysUtils,
  System.Classes,
  System.Variants,
  MVC.Command,
  MVC.ORMParser,
  Data.DB,
  Data.DBCommon;

//获取sql  from部分
function SQL_GetFrom(ASQL: string): string;
//强制替换where段
function SQL_SetWhere(ASQL: string; AWhereSQL: string): string;
//取where段
function SQL_GetWhere(ASQL: string): string;
//追加where条件
function SQL_WhereAnd(ASQL: string; AWhereSQL: string): string;
//强制替换OrderBy段
function SQL_SetOrderBy(ASQL: string; AOrderBySQL: string): string;
//取OrderBy段
function SQL_GetOrderBy(ASQL: string): string;
//附加分页条件
function SQL_SetLimit(ASQL: string; APage, APageSize: Integer): string;
//替换Select from 之间的字段部分
function SQL_SetField(ASQL: string; AField: string): string;

implementation

//TmvcORM尚未完成
type
  TmvcORM = class
  private
    fSQL: string;
    fFieldList: string;
    fTableName: string;
    fWhereSQL: string;
    fGroupSQL: string;
    fOrderSQL: string;
    function getSelectSQL(): string;
  public
    //原始SQL
    property SelectSQL: string read getSelectSQL write fSQL;
    function From(ATableName: string): string;
    function Fields(AFieldsSQL: string): string;
    //fields
    function WhereAnd(AWhereSQL: string): string; overload;
    function WhereAnd(AField: string; AOp: string; AValue: Variant): string; overload;
    function LeftJoin(ATableName: string; OnWhere: string): string;
    function InnerJoin(ATableName: string; OnWhere: string): string;
    function RightJoin(ATableName: string; OnWhere: string): string;
    function OrderBy(AOrderBySQL: string): string;
    function GroupBy(AGroupBySQL: string): string;
    //limit
  end;

function SQL_GetFrom(ASQL: string): string;
begin
  Result := _GetFrom(ASQL, TSQLParser, False, ':');
end;

function SQL_SetWhere(ASQL: string; AWhereSQL: string): string;
begin
  Result := _SetWhere(ASQL, AWhereSQL, TSQLParser, False);
end;

function SQL_GetWhere(ASQL: string): string;
begin
  Result := _GetWhere(ASQL, TSQLParser, false, ':');
end;

function SQL_WhereAnd(ASQL: string; AWhereSQL: string): string;
begin
  Result := _AddWhere(ASQL, AWhereSQL, TSQLParser, False, ':');
end;

function SQL_SetOrderBy(ASQL: string; AOrderBySQL: string): string;
begin
  Result := _SetOrderBy(ASQL, AOrderBySQL, TSQLParser);
end;
function SQL_GetOrderBy(ASQL: string): string;
begin
  Result := _GetOrderBy(ASQL, TSQLParser);
end;

function SQL_SetLimit(ASQL: string;
                      APage: Integer;
                      APageSize: Integer): string;
var
  iPage,
  iPageSize: Integer;
begin
  Result    := ASQL;
  iPage     := aPage;
  iPageSize := aPageSize;

  if iPage <= 0 then
    iPage := 1;
  if iPageSize <= 0 then
    iPageSize := 50;

  if (ipage <= 0) or (iPageSize <= 0) then Exit;
  //这里是firebird语法，各位看官根据自己数据自行修改
  Result := ASQL + Format(' Rows %d to %d', [(iPage-1) * iPageSize + 1, (iPage) * iPageSize]);
end;

function SQL_SetField(ASQL: string; AField: string): string;
begin
  Result := Format('select %s from ', [AField]) + SQL_GetFrom(ASQL);
end;

{TmvcORM}
function TmvcORM.Fields(AFieldsSQL: string): string;
begin
  fFieldList := AFieldsSQL;
end;

function TmvcORM.From(ATableName: string): string;
begin
  fTableName := ATableName;
end;

function TmvcORM.getSelectSQL(): string;
begin
  Result := fSQL;
  if fTableName <> '' then
    Result := Format('Select %s from %s', [fFieldList, fTableName]);
  if fWhereSQL <> '' then
    Result := SQL_WhereAnd(Result, fWhereSQL);
  if fOrderSQL <> '' then
    Result := SQL_SetOrderBy(Result, fOrderSQL);
end;

function TmvcORM.GroupBy(AGroupBySQL: string): string;
begin
  fGroupSQL := AGroupBySQL;
end;

function TmvcORM.InnerJoin(ATableName, OnWhere: string): string;
begin

end;

function TmvcORM.LeftJoin(ATableName: string; OnWhere: string): string;
begin

end;

function TmvcORM.OrderBy(AOrderBySQL: string): string;
begin
  fOrderSQL := AOrderBySQL;
end;

function TmvcORM.RightJoin(ATableName, OnWhere: string): string;
begin

end;

function TmvcORM.WhereAnd(AField, AOp: string; AValue: Variant): string;
var
  s: string;
begin
  s :=  AField + AOp + QuotedStr(VarToStr(AValue));
  WhereAnd(s);
end;

function TmvcORM.WhereAnd(AWhereSQL: string): string;
begin
  if fWhereSQL = '' then
    fWhereSQL := AWhereSQL
  else
    fWhereSQL := fWhereSQL + ' and ' + AWhereSQL;
//  fWhereSQL := SQL_WhereAnd(fWhereSQL, AWhereSQL);
end;



end.

